/* (c) Payam Yousefi. */
function addtag(textareaid,bbtag) {
 	if (document.selection) {
 		document.getElementById(textareaid).focus();
		document.selection.createRange().text = "["+bbtag+"]"+document.selection.createRange().text+"[/"+bbtag+"]";
		document.getElementById(textareaid).focus();
	} else {
		var sstart = document.getElementById(textareaid).selectionStart;
		var ssend = document.getElementById(textareaid).selectionEnd;
		document.getElementById(textareaid).value=document.getElementById(textareaid).value.substring(0, sstart)+"["+bbtag+"]"+document.getElementById(textareaid).value.substring(sstart, ssend)+"[/"+bbtag+"]"+document.getElementById(textareaid).value.substring(ssend);
		document.getElementById(textareaid).setSelectionRange(sstart+bbtag.length+2, ssend+bbtag.length+2);
		document.getElementById(textareaid).focus();
	}
}

function addtext(textareaid, texts) {
 	if (document.selection) { 		
 		document.getElementById(textareaid).focus();
		document.selection.createRange().text = texts;
		document.getElementById(textareaid).focus();
	} else {
		var sstart = document.getElementById(textareaid).selectionStart;
		var ssend = document.getElementById(textareaid).selectionEnd;
		document.getElementById(textareaid).value=document.getElementById(textareaid).value.substring(0, sstart)+texts+document.getElementById(textareaid).value.substring(ssend);
		document.getElementById(textareaid).setSelectionRange(sstart, sstart+texts.length);
		document.getElementById(textareaid).focus();
	}
}

function addurl(textareaid) {
 	if (document.selection) {
 		document.getElementById(textareaid).focus();
 		var url = prompt("Enter URL");
 		if (!url) return;
		document.selection.createRange().text = "[URL="+url+"]"+document.selection.createRange().text+"[/URL]";
		document.getElementById(textareaid).focus();
	} else {
		var sstart = document.getElementById(textareaid).selectionStart;
		var ssend = document.getElementById(textareaid).selectionEnd;
 		var url = prompt("Enter URL","http://");
 		if (!url) return;	
		document.getElementById(textareaid).value=document.getElementById(textareaid).value.substring(0, sstart)+"[URL="+url+"]"+document.getElementById(textareaid).value.substring(sstart, ssend)+"[/URL]"+document.getElementById(textareaid).value.substring(ssend);
		document.getElementById(textareaid).setSelectionRange(sstart+url.length+6, ssend+url.length+6);
		document.getElementById(textareaid).focus();
	}
}

function linkpost(textareaid) {
 	if (document.selection) {
 		document.getElementById(textareaid).focus();
 		var url = prompt("Enter post ID");
 		if (!url) return;
		document.selection.createRange().text = "[POST="+url+"]"+document.selection.createRange().text+"[/POST]";
		document.getElementById(textareaid).focus();
	} else {
		var sstart = document.getElementById(textareaid).selectionStart;
		var ssend = document.getElementById(textareaid).selectionEnd;
 		var url = prompt("Enter post ID");
 		if (!url) return;	
		document.getElementById(textareaid).value=document.getElementById(textareaid).value.substring(0, sstart)+"[POST="+url+"]"+document.getElementById(textareaid).value.substring(sstart, ssend)+"[/POST]"+document.getElementById(textareaid).value.substring(ssend);
		document.getElementById(textareaid).setSelectionRange(sstart+url.length+7, ssend+url.length+7);
		document.getElementById(textareaid).focus();
	}
}

function linkpage(textareaid) {
 	if (document.selection) {
 		document.getElementById(textareaid).focus();
 		var url = prompt("Enter page ID");
 		if (!url) return;
		document.selection.createRange().text = "[PAGE="+url+"]"+document.selection.createRange().text+"[/PAGE]";
		document.getElementById(textareaid).focus();
	} else {
		var sstart = document.getElementById(textareaid).selectionStart;
		var ssend = document.getElementById(textareaid).selectionEnd;
 		var url = prompt("Enter page ID");
 		if (!url) return;	
		document.getElementById(textareaid).value=document.getElementById(textareaid).value.substring(0, sstart)+"[PAGE="+url+"]"+document.getElementById(textareaid).value.substring(sstart, ssend)+"[/PAGE]"+document.getElementById(textareaid).value.substring(ssend);
		document.getElementById(textareaid).setSelectionRange(sstart+url.length+7, ssend+url.length+7);
		document.getElementById(textareaid).focus();
	}
}

function clearsearch() {
	document.getElementById("attachment_search_text").value="";
}